
var gc_txtHelp = "Enter stock symbol";
var gc_txtStockBox = "<b>Stocks:</b>";
var gc_txtRefreshLabel = "<b>Refresh Time:</b>";
var gc_txtSeconds = "(seconds)";

var gc_nMinRefreshSeconds = 2;     // 10 seconds is the lowest (fastest) refresh setting
var gc_nMaxRefreshSeconds = 3600;   // 3,600 seconds (1 hour) is the highest (slowest) refresh setting

function settingsInit() {

    // Delegate for the settings closing event. 
    if (typeof (System) != "undefined") {//
        System.Gadget.onSettingsClosing = SettingsClosing;
    }

    pHelp.innerHTML = gc_txtHelp;
    pStock.innerHTML = gc_txtStockBox;
    pRefresh.innerHTML = gc_txtRefreshLabel;
    pSeconds.innerHTML = gc_txtSeconds;

    pPollingTime.innerHTML = "<b>Polling Time:</b>";
    pSeconds1.innerHTML = gc_txtSeconds;

    SettingsManager.loadFile();
    var nRefreshTime = SettingsManager.getValue("Refresh", "RefreshTime");
    var nPollingTime = SettingsManager.getValue("Refresh", "PollingTime");

    var txtStockSymbolsInfo = SettingsManager.getValue("Stocks", "StockSymbolsInfo");

    if (nRefreshTime == "" || nRefreshTime == null || nRefreshTime == "undefined" || nRefreshTime < gc_nMinRefreshSeconds) nRefreshTime = gc_nMinRefreshSeconds;
    if (nRefreshTime > gc_nMaxRefreshSeconds) nRefreshTime = gc_nMaxRefreshSeconds;
    if (nPollingTime == "" || nPollingTime == null || nPollingTime == "undefined")
        nPollingTime = 15;

    txtRefresh.value = nRefreshTime;
    txtPolling.value = nPollingTime;

    var colStocks = txtStockSymbolsInfo.split(";");
    var n = 0;
    for (n in colStocks) {
        if (colStocks[n] != "" && colStocks[n] != "undefined")
            selStocks.options.add(new Option(colStocks[n], colStocks[n]));
    }

    //TabSelect("SettingsContent");
}

function SettingsClosing(event) {
    if (event.closeAction == event.Action.commit) {

        var nRefreshTime = txtRefresh.value;
        var nPollingTime = txtPolling.value;
        if (nRefreshTime == "" || nRefreshTime == null || nRefreshTime == "undefined" || nRefreshTime < gc_nMinRefreshSeconds) nRefreshTime = gc_nMinRefreshSeconds;
        if (nRefreshTime > gc_nMaxRefreshSeconds) nRefreshTime = gc_nMaxRefreshSeconds;

        var txtStockSymbolsInfo = "";
        var colStocks = selStocks.all.tags("OPTION");

        var n;

        for (n in colStocks) {
            if (colStocks[n] != null && colStocks[n].innerText != null &&
			    colStocks[n].innerText != "" && colStocks[n].innerText != "undefined") {
                if (txtStockSymbolsInfo == "") txtStockSymbolsInfo = colStocks[n].innerText;
                else txtStockSymbolsInfo += ";" + colStocks[n].innerText;
            }
        }
        SettingsManager.setValue("Refresh", "RefreshTime", nRefreshTime);
        SettingsManager.setValue("Refresh", "PollingTime", nPollingTime);
        SettingsManager.setValue("Stocks", "StockSymbolsInfo", txtStockSymbolsInfo);
        SettingsManager.saveFile();
        event.cancel = false;
    }
    else {
        event.cancel = true;
    }
}

function TabSelect(txtTab) {
    if (txtTab == "SettingsContent") {
        AboutContent.style.display = "none";
        AboutTab.style.borderBottomColor = "Black";

        SettingsContent.style.display = "inline";
        SettingsTab.style.borderBottomColor = "#F0F0F0";

        txtNewStock.focus();
    }
    else if (txtTab == "AboutContent") {
        SettingsContent.style.display = "none";
        SettingsTab.style.borderBottomColor = "Black";

        AboutContent.style.display = "inline";
        AboutTab.style.borderBottomColor = "#F0F0F0";
    }
}

function AddStock() {
    if (txtNewStock.value != "" && txtNewStock.value != "undefined" && txtNewStock.value != null) {
        selStocks.options.add(new Option(txtNewStock.value, txtNewStock.value));
        selStocks.selectedIndex = selStocks.length - 1;
        txtNewStock.value = "";
        txtNewStock.focus();
    }
}

function RemoveStock() {
    var sel = selStocks.selectedIndex;
    for (i = selStocks.length - 1; i >= 0; i--) {
        if (selStocks.options[i].selected) {
            selStocks.options[i] = null;
            sel = i;
        }
    }
    if (sel == selStocks.length) sel--;
    selStocks.selectedIndex = sel;
    //	selStocks.focus();
    txtNewStock.focus();
}

function MoveStockUp() {
    if (selStocks.length != -1 && selStocks.length != 0) {  // If the list is not empty or has only one item
        var selected = selStocks.selectedIndex;
        if (selected != -1 && selected != 0) {  // If an item is selected and is not the first item
            // Get the text/value of the one directly above the hightlighted entry as
            // well as the highlighted entry; then flip them
            var moveText1 = selStocks[selected - 1].text;
            var moveText2 = selStocks[selected].text;
            var moveValue1 = selStocks[selected - 1].value;
            var moveValue2 = selStocks[selected].value;
            selStocks[selected].text = moveText1;
            selStocks[selected].value = moveValue1;
            selStocks[selected - 1].text = moveText2;
            selStocks[selected - 1].value = moveValue2;
            selStocks.selectedIndex = selected - 1; // Select the one that was selected before
        }
    }
}

function MoveStockDown() {
    if (selStocks.length != -1 && selStocks.length != 0) {  // If the list is not empty or has only one item
        var selected = selStocks.selectedIndex;
        if (selected != -1 && selected != selStocks.length - 1) {  // If an item is selected and is not the first item
            // Get the text/value of the one directly above the hightlighted entry as
            // well as the highlighted entry; then flip them
            var moveText1 = selStocks[selected + 1].text;
            var moveText2 = selStocks[selected].text;
            var moveValue1 = selStocks[selected + 1].value;
            var moveValue2 = selStocks[selected].value;
            selStocks[selected].text = moveText1;
            selStocks[selected].value = moveValue1;
            selStocks[selected + 1].text = moveText2;
            selStocks[selected + 1].value = moveValue2;
            selStocks.selectedIndex = selected + 1; // Select the one that was selected before
        }
    }
}